﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MMITPinGen.Entities;
using MMITDomain.Entities;
using MMITDomain;
using MMITDomain.Concrete;
using MMITCore.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Web.Security;

namespace MMITPinGen
{
    public partial class ManagePartner : System.Web.UI.Page
    {
        EFPartnerRepository PartnerRepository = new EFPartnerRepository();
        Partner p;
        SynkronLogger Log = new SynkronLogger("MMITPINGen");
        Dictionary<string, object> d = new Dictionary<string, object>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                
                try 
                {
                    List<string> roles = Roles.GetRolesForUser(User.Identity.Name).ToList();
                    var r = from p in roles
                            where p == "MMIT Admin"
                            select p;
                    if (r.Count() == 0)
                    {
                        lblMsg.Text = "You do not have permission to perform this function. Please contact Customer Service";
                        tblForm.Visible = false;
                        btnNew.Visible = false;
                        btnList.Visible = false;
                    }
                    else
                    {
                        this.txtName.Text = string.Empty;
                        this.txtAddress.Text = string.Empty;
                        this.txtDesc.Text = string.Empty;
                        this.tblForm.Visible = true;
                        this.txtName.ReadOnly = false;
                        drpStatus.SelectedValue = Convert.ToString(1);
                        List<Partner> lp = (from p in PartnerRepository.Partners
                                            select p).ToList();
                        grdPartners.DataSource = lp;
                        grdPartners.DataBind();
                        this.tblForm.Visible = true;
                        this.grdPartners.Visible = false;
                    }
                     

                }
                catch (Exception y)
                {
                    //strErrorCode = "A User with this Mobile Number exists";
                    //d.Clear();
                    //d.Add("Membership Details", Log.Desc(c));
                    //d.Add("Registration Error : ", strErrorCode);
                    Log.WriteCritical(21300027, "error", y);
                }
               

            }

        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            this.tblForm.Visible = true;
            this.grdPartners.Visible = false;
            this.txtName.Text = string.Empty;
            this.txtName.ReadOnly = false;
            this.txtAddress.Text = string.Empty;
            this.txtDesc.Text = string.Empty;
            drpStatus.SelectedValue = Convert.ToString(1);
            btnSubmit.Text = "Submit";

        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            this.tblForm.Visible = false;
            this.grdPartners.Visible = true;
            txtName.ReadOnly = true;
        }
        public static bool UserNameExists(string yourName)
        {
            return Membership.GetUser(yourName) != null;
        }  
        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if(btnSubmit.Text=="Submit")
            {
                if (UserNameExists(this .txtName.Text))
                {
                    d.Clear();
                    d.Add("Partner Already Exists", Log.Desc(p));
                    Log.WriteInformation(21900028, "", d);
                }
                else
                {
                    Membership.CreateUser(txtName.Text, txtPassword.Text);
                    p = new Partner {Name=txtName.Text, Address = txtAddress.Text, Description = txtDesc.Text, Status = Convert.ToInt32(drpStatus.SelectedValue) };
                    PartnerRepository.SavePartner(p);
                    EFAccountRepository ef = new EFAccountRepository();
                    MMITDomain.Entities.Account a = new MMITDomain.Entities.Account();
                    a.Name = p.Name;
                    a.AccountTypeId = 5;
                    a.Status = 6;
                    a.CurrencyId = 1;
                    a.CustomerId = 0;
                    //a.CustomerId = ;
                    ef.SaveAccount(a);
                    Roles.AddUserToRole(txtName.Text, "Partner");  
                    d.Clear();
                    d.Add("Partner Created", Log.Desc(p));
                    Log.WriteInformation(21900027, "", d);
                
                }
               
                
            }
            else
            {
                var t = grdPartners.SelectedItems;
                
                HiddenField k = (HiddenField)t[0].FindControl("HiddenField1");
                p = new Partner {Id = Convert.ToInt32(k.Value), Name =txtName.Text, Address = txtAddress.Text, Description = txtDesc.Text, Status = Convert.ToInt32(drpStatus.SelectedValue) };
                PartnerRepository.SavePartner(p);
                d.Clear();
                d.Add("Partner Updated", Log.Desc(p));
                Log.WriteInformation(21903027, "", d);

            }

           

               List<Partner> lp = ( from t in  PartnerRepository.Partners 
                                             select t).ToList();
            grdPartners.DataSource = lp;
            grdPartners.DataBind();
            this.tblForm.Visible = false;
            this.grdPartners.Visible = true;
            this.txtName.ReadOnly = false;
            btnSubmit.Text = "Submit";

        }

        protected void grdPartners_SelectedIndexChanged(object sender, EventArgs e) 
        {
            var t = grdPartners.SelectedItems;
           HiddenField k =  (HiddenField)t[0].FindControl("HiddenField1");
           RequiredFieldValidator2.Enabled = false;
           RequiredFieldValidator3.Enabled = false;
            p = PartnerRepository.GetPartnerById (Convert.ToInt32( k.Value));
            txtName.Text = p.Name;
            txtName.ReadOnly = true;
            txtAddress.Text = p.Address;
            txtDesc.Text = p.Description;
            drpStatus.SelectedValue = Convert.ToString( p.Status);
            tblForm.Visible = true;
            grdPartners.Visible = false;
            btnSubmit.Text = "Edit";

            
        }

       


        

       

       

       
    }
}